#project('spice-common', 'c',
#        meson_version : '>= 0.49.0',
#        license : 'LGPLv2.1',
#        default_options : ['warning_level=2'])

project(spice-common)

option(alignment-checks "Enable runtime checks for cast alignment" OFF)
option(extra-checks "Enable extra checks on code" FALSE)
my_multi_option(instrumentation "no" "Enable instrumentation" recorder agent no)
my_multi_option(generate-code "all" "Which code should be built" all server client)
set(spice-protocol-version "0.1" CACHE STRING "Minimal requested SPICE protocol version")
option(tests "Enable SPICE tests" FALSE)

#
#if not meson.is_subproject()
#  warning('This project is only intended to be used as a subproject!')
#endif
#
## some global vars
#spice_common_global_cflags = ['-DG_LOG_DOMAIN="Spice"',
#                              '-Wno-unused-parameter']

set(spice_common_global_cflags
        # "-DG_LOG_DOMAIN=\"Spice\""
        "-Wno-unused-parameter"
)

if (alignment-checks)
        list(APPEND spice_common_global_cflags " -DSPICE_DEBUG_ALIGNMENT")
endif ()

#
#spice_common_deps = []
set(spice_common_deps )
#spice_common_include = include_directories('.')
set(spice_common_include ${CMAKE_CURRENT_SOURCE_DIR})
list(APPEND spice_common_include ${CMAKE_CURRENT_BINARY_DIR})
#
#spice_proto = files('spice.proto')
#spice_codegen = files('spice_codegen.py')
#spice_codegen_files = [spice_codegen]
#
if (extra-checks)
        set(ENABLE_EXTRA_CHECKS 1) #for config.h
        set(ENABLE_EXTRA_CHECKS_PS ON PARENT_SCOPE)
endif()
if (${CMAKE_C_BYTE_ORDER} STREQUAL "BIG_ENDIAN")
        set(WORDS_BIGENDIAN 1) #for config.h
endif()

if (instrumentation STREQUAL "recorder")
        set(ENABLE_RECORDER 1) #for config.h
elseif(instrumentation STREQUAL "agent")
        set(ENABLE_AGENT_INTERFACE 1) #for config.h
endif()

set(spice_common_generate_client_code FALSE)
set(spice_common_generate_server_code FALSE)
message("will judge generate cliend code true")
if (${generate-code} STREQUAL "all" OR ${generate-code} STREQUAL "client")
    message("generate cliend code true")
    set(spice_common_generate_client_code TRUE)
endif()
if (${generate-code} STREQUAL "all" OR ${generate-code} STREQUAL "server")
    message("generate server code true")
    set(spice_common_generate_server_code TRUE)
endif()

include(CheckIncludeFiles)
include(CheckFunctionExists)

check_include_files(alloca.h have_alloca_h)
if (have_alloca_h)
    set(HAVE_ALLOCA_H 1) #for config.h
endif()
check_include_files(arpa/inet.h have_arpa_inet_h)
if (have_arpa_inet_h)
    set(HAVE_ARPA_INET_H 1) #for config.h
endif()
check_include_files(dlfcn.h have_dlfcn_h)
if (have_dlfcn_h)
    set(HAVE_DLFCN_H 1) #for config.h
endif()
check_include_files(inttypes.h have_inttypes_h)
if (have_inttypes_h)
    set(HAVE_INTTYPES_H 1) #for config.h
endif()
check_include_files(netinet/in.h have_netinet_in_h)
if (have_netinet_in_h)
    set(HAVE_NETINET_IN_H 1) #for config.h
endif()
check_include_files(stdlib.h have_stdlib_h)
if (have_stdlib_h)
    set(HAVE_STDLIB_H 1) #for config.h
endif()
check_include_files(sys/socket.h have_sys_socket_h)
if (have_sys_socket_h)
    set(HAVE_SYS_SOCKET_H 1) #for config.h
endif()
check_include_files(sys/stat.h have_sys_stat_h)
if (have_sys_stat_h)
    set(HAVE_SYS_STAT_H 1) #for config.h
endif()
check_include_files(sys/types.h have_sys_types_h)
if (have_sys_types_h)
    set(HAVE_SYS_TYPES_H 1) #for config.h
endif()
check_include_files(unistd.h have_unistd_h)
if (have_unistd_h)
    set(HAVE_UNISTD_H 1) #for config.h
endif()
check_include_files(regex.h have_regex_h)
if (have_regex_h)
    set(HAVE_REGEX_H 1) #for config.h
endif()
check_include_files(sys/mman.h have_sys_mman_h)
if (have_sys_mman_h)
    set(HAVE_SYS_MMAN_H 1) #for config.h
endif()

check_function_exists(alloca have_alloca)
if (have_alloca)
    set(HAVE_ALLOCA 1) #for config.h
endif()
check_function_exists(sigaction have_sigaction)
if (have_sigaction)
    set(HAVE_SIGACTION 1) #for config.h
endif()
check_function_exists(drand48 have_drand48)
if (have_drand48)
    set(HAVE_DRAND48 1) #for config.h
endif()
check_function_exists(setlinebuf have_setlinebuf)
if (have_setlinebuf)
    set(HAVE_SETLINEBUF 1) #for config.h
endif()


#
## FIXME
## check for libm, as workaround for https://github.com/mesonbuild/meson/issues/3740
#libm = compiler.find_library('m', required : false)
#if libm.found()
#  spice_common_deps += libm
#endif
#
##
## check for mandatory dependencies
##
#glib_version = '2.38'
#glib_version_info = '>= @0@'.format(glib_version)
#
#spice_protocol_version = '0.14.2'
#
#spice_protocol_version_req = get_option('spice-protocol-version')
#if spice_protocol_version_req.version_compare('> @0@'.format(spice_protocol_version))
#  spice_protocol_version = spice_protocol_version_req
#endif
set(glib_version "2.38")
set(spice_protocol_version "0.14.2")
if (${spice-protocol-version} STRGREATER ${spice_protocol_version})
        message("spice-protocol-version is " ${spice-protocol-version})
        set(spice_protocol_version ${spice-protocol-version})
endif()
message("spice_protocol_version is " ${spice_protocol_version})

set(HAVE_OPUS 1) #for config.h
#
## Python
#if spice_common_generate_client_code or spice_common_generate_server_code
#  py_module = import('python')
#  python = py_module.find_installation('python3')
#
#  if get_option('python-checks')
#    foreach module : ['six', 'pyparsing']
#      message('Checking for python module @0@'.format(module))
#      cmd = run_command(python, '-c', 'import @0@'.format(module))
#      if cmd.returncode() != 0
#        error('Python module @0@ not found'.format(module))
#      endif
#    endforeach
#  endif
#endif
if (${spice_common_generate_client_code} OR ${spice_common_generate_server_code})


endif()

#
## smartcard check
#smartcard_dep = dependency('libcacard', required : get_option('smartcard'), version : '>= 2.5.1')
#if smartcard_dep.found()
#  spice_common_deps += smartcard_dep
#  spice_common_config_data.set('USE_SMARTCARD', '1')
#endif
#
##
## global C defines
##
#glib_encoded_version = 'GLIB_VERSION_@0@'.format(glib_version.underscorify())
#spice_common_global_cflags += ['-DGLIB_VERSION_MIN_REQUIRED=@0@'.format(glib_encoded_version),
#                               '-DGLIB_VERSION_MAX_ALLOWED=@0@'.format(glib_encoded_version)]
#
#add_project_arguments(compiler.get_supported_arguments(spice_common_global_cflags),
#                      language : 'c')
#
underscorify(glib_version glib_version_underscorify)
set(glib_encoded_version "GLIB_VERSION_${glib_version_underscorify}")
message("glib_encoded_version is " ${glib_encoded_version})

list(APPEND spice_common_global_cflags
    " -DGLIB_VERSION_MIN_REQUIRED=${glib_encoded_version}"
    )
list(APPEND spice_common_global_cflags
    " -DGLIB_VERSION_MAX_ALLOWED=${glib_encoded_version} "
    )
set(spice_common_global_cflags_ps ${spice_common_global_cflags} PARENT_SCOPE)

#subdir('python_modules')
#subdir('common')
#if get_option('tests')
#  subdir('tests')
#endif
#subdir('docs')
add_subdirectory(common)
if (tests)
    add_subdirectory(tests)
endif()

configure_file(config.h.cmake config.h @ONLY)
set(spice_common_include_ps ${spice_common_include_ps} PARENT_SCOPE)
